using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPrimaryAttackState : PlayerState
{
    public int comboCounter{get;private set;}// 连击数
    private float lasyTimeAttacked;// 最后一次攻击的时间
    private float comboWindow = 2;// 连击间隔

    public PlayerPrimaryAttackState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
    {
    }

    public override void Enter()
    {
        base.Enter();
        xInput = Input.GetAxisRaw("Horizontal");//重置输入，否则有自动转向的bug
        if (comboCounter > 2 || Time.time > lasyTimeAttacked + comboWindow)// 连击大于2或攻击时间大于最后一次攻击时间+连击间隔
        {
            comboCounter = 0;
        }
        player.anim.SetInteger("ComboCounter", comboCounter);// 设置动画连击数
        // player.anim.speed = 1.2f;//增加动画速度（攻速）
        //改变攻击方向
        float attackDir = player.facingDir;//attackMovement的x轴不要赋值0，否则无法转向
        if (xInput != 0) attackDir = xInput;

        // 攻击固定移动模式
        player.SetVelocity(player.attackMovement[comboCounter].x * attackDir, player.attackMovement[comboCounter].y);

        stateTimer = .1f;// 轻微惯性，保护手感
    }

    public override void Exit()
    {
        base.Exit();

        player.StartCoroutine("BusyFor", .2f);//让角色忙防止连续攻击进入移动状态--没有攻击固定移动模式的话影响手感

        comboCounter++;// 攻击连击数增加
        lasyTimeAttacked = Time.time;// 最后一次攻击的时间
    }

    public override void Update()
    {

        base.Update();
        // 在正常状态下攻击静止移动
        if (stateTimer < 0)
        {
            player.SetZeroVelocity();
        }

        // 动画结束退出攻击状态
        if (triggerCalled)
        {
            stateMachine.ChangeState(player.idleState);
        }
    }
}
